package com.mdd.admin.controller.order;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.mdd.admin.config.aop.Log;
import com.mdd.admin.service.IOrderService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.order.OrderBatchValidate;
import com.mdd.admin.validate.order.OrderSearchValidate;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.validator.annotation.IDMust;
import org.checkerframework.checker.units.qual.C;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 订单控制层
 * @author shenjian
 * @createTime 2023年02月11日 21:40:00
 */
@RestController
@RequestMapping("api/order")
@CrossOrigin
public class OrderController {

    @Resource
    IOrderService iOrderService;

    /**
     * 订单列表
     *
     * @author shenjian
     * @param searchValidate 搜索参数
     * @return AjaxResult<PageResult<Object>>
     */
    @Log(title = "订单列表")
    @PostMapping(value = "/list")
    public AjaxResult<Object> list(@Validated @RequestBody OrderSearchValidate searchValidate) {
        return AjaxResult.success(iOrderService.list(searchValidate));
    }

    /**
     * 订单详情
     *
     * @author shenjian
     * @return AjaxResult<PageResult<Object>>
     */
    @Log(title = "订单详情")
    @GetMapping(value = "/detail")
    public AjaxResult<Object> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        return AjaxResult.success(iOrderService.getOrderDetail(id));
    }


    /**
     * 截止退款
     *
     * @author shenjian
     * @param validate
     * @return AjaxResult<PageResult<Object>>
     */
    @Log(title = "截止退款")
    @PostMapping(value = "/notRefund")
    public AjaxResult<Object> notRefund(@Validated @RequestBody OrderBatchValidate validate) {
        iOrderService.notRefund(validate);
        return AjaxResult.success();
    }

    /**
     * 一键配送
     *
     * @author shenjian
     * @return AjaxResult<PageResult<Object>>
     */
    @Log(title = "一键配送")
    @GetMapping(value = "/allDelivery")
    public AjaxResult<Object> allDelivery() {
        iOrderService.allDelivering();
        return AjaxResult.success();
    }

    /**
     * 一键送达
     *
     * @author shenjian
     * @return AjaxResult<PageResult<Object>>
     */
    @Log(title = "一键送达")
    @GetMapping(value = "/allDelivered")
    public AjaxResult<Object> allDelivered() {
        iOrderService.allDelivered();
        return AjaxResult.success();
    }

    /**
     * 配送中
     *
     * @author shenjian
     * @param validate
     * @return AjaxResult<PageResult<Object>>
     */
    @Log(title = "配送中")
    @PostMapping(value = "/delivering")
    public AjaxResult<Object> delivering(@Validated @RequestBody OrderBatchValidate validate) {
        iOrderService.delivering(validate);
        return AjaxResult.success();
    }

    /**
     * 已送达
     *
     * @author shenjian
     * @param validate
     * @return AjaxResult<PageResult<Object>>
     */
    @Log(title = "已送达")
    @PostMapping(value = "/delivered")
    public AjaxResult<Object> delivered(@Validated @RequestBody OrderBatchValidate validate) {
        iOrderService.delivered(validate);
        return AjaxResult.success();
    }

    /**
     * 退款
     *
     * @author shenjian
     * @param validate
     * @return AjaxResult<PageResult<Object>>
     */
    @Log(title = "退款")
    @PostMapping(value = "/refund")
    public AjaxResult<Object> refund(@Validated @RequestBody OrderBatchValidate validate) {
        iOrderService.refund(validate);
        return AjaxResult.success();
    }

    /**
     * 导出
     * @param date
     * @param response
     */
    @Log(title = "导出")
    @GetMapping(value = "/export")
    public void export(@RequestParam("date") String date,
                       HttpServletResponse response) {
        Assert.notEmpty(date, "date不能为空");
        iOrderService.export(date, response);
    }
}
